from django.shortcuts import render

from django.http import HttpResponse,HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth.decorators import login_required

from sign.models import Staff
# Create your views here.

'''
def index(request):
    return HttpResponse("Hello Django!")
'''

def index(request):
    return render(request,"index.html")

#登陆处理
def login_action(request):
    if request.method == 'POST':
        username = request.POST.get('username','')
        password = request.POST.get('password','')
        user = auth.authenticate(username=username,password=password)
        if user is not None:
            auth.login(request,user)
            response = HttpResponseRedirect('/event_manage/')
            #添加浏览器cookie
            response.set_cookie('user',username,3600)
            return response
        else:
            return render(request,'index.html',{'error':'name or password is wrong'})    

#发布会管理
@login_required
def event_manage(request):
    staff_list = Staff.objects.all()
    #读取浏览器cookie
    username = request.COOKIES.get('user','')
    return render(request,'event_manage.html',{'user':username,"staffs":staff_list})

